@echo off
setlocal
call "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars32.bat"
setlocal enabledelayedexpansion

del /q *.exe *.pdb

set CPPFLAGS=/nologo /c /GS- /Gy- /GR- /Oi- /D_NO_CRT_STDIO_INLINE /DWIN32_LEAN_AND_MEAN /EHsc
set CPPFLAGS=%CPPFLAGS% /O1
:: set CPPFLAGS=%CPPFLAGS% /Zi

set LDFLAGS=/nologo /incremental:no /nodefaultlib /safeseh:no /merge:.rdata=.text /EMITPOGOPHASEINFO /entry:start /align:16 /OUT:webui.exe
:: set LDFLAGS=%LDFLAGS% /subsystem:console
set LDFLAGS=%LDFLAGS% /subsystem:windows
:: set LDFLAGS=%LDFLAGS% /Debug
set LIBS=kernel32.lib user32.lib Shlwapi.lib Advapi32.lib Ws2_32.lib httpapi.lib msvcrt.lib

echo LIBRARY> msvcrt.def
echo EXPORTS>> msvcrt.def
echo memcpy>> msvcrt.def
echo wcslen>> msvcrt.def
echo wcscmp>> msvcrt.def
echo memset>> msvcrt.def
echo strlen>> msvcrt.def
echo malloc>> msvcrt.def
echo free>> msvcrt.def
echo wcscpy_s>> msvcrt.def
echo vsprintf_s>> msvcrt.def
echo vswprintf_s>> msvcrt.def
echo wprintf>> msvcrt.def
echo _vscprintf>> msvcrt.def
echo _vscwprintf>> msvcrt.def
echo _EH_prolog>> msvcrt.def
echo __CxxFrameHandler3>> msvcrt.def
echo puts>> msvcrt.def
echo wcsncmp>> msvcrt.def
echo wcsstr>> msvcrt.def
echo wcschr>> msvcrt.def
echo wcstok_s>> msvcrt.def
echo realloc>> msvcrt.def

lib /nologo /machine:X86 /def:msvcrt.def /out:msvcrt.lib

set OBJS=

for %%i in (*.cpp) do (
    cl %CPPFLAGS% %%i || goto err
    set OBJS=!OBJS! %%~ni.obj
)
link %LDFLAGS% !OBJS! %LIBS% || goto err


del /q *.obj *.exp *.lib *.def
exit /b 0

:err
exit /b 1
